package com.yyyit.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by lsjun on 16-3-27.
 */
public class HttpRequest {
    InputStream is = null;
    HttpURLConnection httpUrlConn = null;
    StringBuffer sbContent = new StringBuffer();

    public String HttpRequest(String st) {

        try {
            URL url = new URL(st);
            //打开连接
            httpUrlConn = (HttpURLConnection) url.openConnection();
            //获取连接状态
            int rc = httpUrlConn.getResponseCode();


            //连接成功
            if (rc == HttpURLConnection.HTTP_OK) {
                //获取数据，先得到输入流
                is = httpUrlConn.getInputStream();

                BufferedReader buffer = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                String inputLine = null;


                while ((inputLine = buffer.readLine()) != null) {
                    sbContent.append(inputLine + "\n");
                }


                buffer.close();
            }


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            if (is != null) {
                try {
                    is.close();
                    is = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (httpUrlConn != null) {
                //关闭连接
                httpUrlConn.disconnect();
                httpUrlConn = null;
            }
            return sbContent.toString();
        }
    }


}
